package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.mine.contract.AccountSecurityContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.presenter.AccountSecurityPresenter;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import javax.inject.Inject;

/**
 *  Created by amy on 2017/11/9.
 */

public class AccountSecurityActivity extends BaseToolbarActivity implements AccountSecurityContract.View{
    private RelativeLayout mRlSecurityPaymentPassword;
    private CheckBox mCbSecuritySmallAccount;
    private CheckBox mCbSecuritySmsNotification;
    private UserToken mUserToken;
    private RelativeLayout mRlSecurityFreeAccount;
    private String isSmallAccountOpen = Constants.PASSWORD_UNSELECT;
    private String isSmsNotifyOpen = Constants.PASSWORD_UNSELECT;
    @Inject
    AccountSecurityPresenter mAccountSecurityPresenter;
    private String mUserPhoneNumber;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("账号安全");
        initId();
        isEnable(false);
        initRXClick();
        initUserInfo();
        DaggerMineComponent.builder().mineModule(new MineModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);

    }

    private void initUserInfo() {
        Intent intent = getIntent();
        mUserPhoneNumber = intent.getStringExtra("userPhoneNumber");
        mUserToken = InfoProvider.getUserToken(this);
        if (mUserToken != null){
            setCbStatus();
        }
    }

    private void setCbStatus() {
        if (mUserToken.getSmall_direct_flag().equals(Constants.PASSWORD_SELECT)){
            mCbSecuritySmallAccount.setBackgroundResource(R.mipmap.pay_withoutpw_select);
            mUserToken.setSmall_direct_flag(Constants.PASSWORD_SELECT);
            mRlSecurityFreeAccount.setVisibility(View.GONE);
        }else {
            mCbSecuritySmallAccount.setBackgroundResource(R.mipmap.pay_withoutpw_unselect);
            mUserToken.setSmall_direct_flag(Constants.PASSWORD_UNSELECT);
            mRlSecurityFreeAccount.setVisibility(View.VISIBLE);
        }

        if (mUserToken.getSms_notify_flag().equals(Constants.PASSWORD_SELECT) ){
            mCbSecuritySmsNotification.setBackgroundResource(R.mipmap.pay_withoutpw_select);
            mUserToken.setSms_notify_flag(Constants.PASSWORD_SELECT);

        }else {
            mCbSecuritySmsNotification.setBackgroundResource(R.mipmap.pay_withoutpw_unselect);
            mUserToken.setSms_notify_flag(Constants.PASSWORD_UNSELECT);
        }
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.rl_security_payment_password:
                JumpUtil.jumpToActivityWithPhoneNumber(this,SetPayPasswordActivity.class);
                break;
            case R.id.cb_security_smallAccount:
                //小额免密
                changSmallAccountStatus();
                PreferenceUtils.saveObject(this,Constants.USER_TOKEN,mUserToken);
                mAccountSecurityPresenter.saveSmallAccountSetting(mUserToken);
                break;
            case R.id.cb_security_sms_notification:
                //短信通知
               changeSmsNotification();
                PreferenceUtils.saveObject(this,Constants.USER_TOKEN,mUserToken);
                mAccountSecurityPresenter.saveSmallAccountSetting(mUserToken);
                break;
        }

    }

    private void changeSmsNotification() {
        if (mUserToken.getSms_notify_flag().equals(Constants.PASSWORD_SELECT) ){
            mCbSecuritySmsNotification.setBackgroundResource(R.mipmap.pay_withoutpw_unselect);
            mUserToken.setSms_notify_flag(Constants.PASSWORD_UNSELECT);

            isSmsNotifyOpen = Constants.PASSWORD_UNSELECT;
        }else {
            mCbSecuritySmsNotification.setBackgroundResource(R.mipmap.pay_withoutpw_select);
            mUserToken.setSms_notify_flag(Constants.PASSWORD_SELECT);
            isSmsNotifyOpen = Constants.PASSWORD_SELECT;
        }
    }

    private void changSmallAccountStatus() {
        if (mUserToken.getSmall_direct_flag().equals(Constants.PASSWORD_SELECT)){
            mCbSecuritySmallAccount.setBackgroundResource(R.mipmap.pay_withoutpw_unselect);
            mUserToken.setSmall_direct_flag(Constants.PASSWORD_UNSELECT);
            mRlSecurityFreeAccount.setVisibility(View.VISIBLE);
            isSmallAccountOpen = Constants.PASSWORD_UNSELECT;


        }else {
            mCbSecuritySmallAccount.setBackgroundResource(R.mipmap.pay_withoutpw_select);
            mUserToken.setSmall_direct_flag(Constants.PASSWORD_SELECT);
            mRlSecurityFreeAccount.setVisibility(View.GONE);
            isSmallAccountOpen = Constants.PASSWORD_SELECT;
        }
    }

    private void initRXClick() {
        RxUtils.bindEvents(mRlSecurityPaymentPassword,this);
        RxUtils.bindEvents(mCbSecuritySmallAccount,this);
        RxUtils.bindEvents(mCbSecuritySmsNotification,this);
    }

    private void initId() {
       mRlSecurityPaymentPassword = getViewById(R.id.rl_security_payment_password);
       mCbSecuritySmallAccount = getViewById(R.id.cb_security_smallAccount);
       mCbSecuritySmsNotification = getViewById(R.id.cb_security_sms_notification);
       mRlSecurityFreeAccount = getViewById(R.id.rl_security_free_account);
    }

    @Override
    protected int setView() {
        return R.layout.activity_account_security;
    }

    @Override
    protected void onRefreshData() {

    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void settingSuccess() {
        TipUtils.showToast(App.getInstance(),"设置成功",3);
    }


}
